package ce.com.cenewbluesdk.entity.k2;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.uitl.ByteUtil;
import ce.com.cenewbluesdk.uitl.CRC16;

/**
 * Created by Administrator on 2016/11/24 0024.
 */
public class K2_UserCard {


    private static final int USER_CARD_PERSON = 0;    //个人名片
    private static final int USER_CARD_BUSINESS = 1;    //商业名片
    private static final int USER_CARD_VIP = 2;

    private static final int cardL = 2048;
    private static final int sendItemL = 440;

    private int crc16;
    private int type;
    private byte[] bytes = new byte[cardL + 3];

    private int sendIndex;


    public K2_UserCard(int type, byte[] bs) {
        if (bs.length != cardL) {
            return;
        }
        this.type = type;
        bytes[2] = (byte) type;
        System.arraycopy(bs, 0, bytes, 3, bs.length);

        crc16 = CRC16.crc16(bytes, 3);
        System.arraycopy(ByteUtil.int2bytes2(crc16), 0, bytes, 0, 2);

    }

    public CEDevData getCeData() {
        CEDevData ceDevData = new CEDevData(CEBC.K2.CMD_TYPE_SEND, CEBC.K2.DATA_TYPE_USER_CARD);
        int datal = 0;
        if (sendIndex + sendItemL < cardL + 3) {
            datal = sendItemL;
        } else {
            datal = cardL + 3 - sendIndex;
        }
        if (datal == 0) {
            return null;
        }





        ceDevData.setItemL(datal+7);
        ceDevData.setItemNumber(1);
        byte sendb[] = new byte[datal+7];
        System.arraycopy(bytes, sendIndex, sendb, 7, datal);

        sendb[0]= (byte) type;
        System.arraycopy(ByteUtil.int2bytes2(2051), 0, sendb, 1, 2);
        System.arraycopy(ByteUtil.int2bytes2(datal), 0, sendb, 3, 2);
        System.arraycopy(ByteUtil.int2bytes2(sendIndex), 0, sendb, 5, 2);
        ceDevData.setData(sendb);
        sendIndex=sendIndex+datal;

        return ceDevData;
    }
}
